package demo.practice.competition;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class P5847 {

    public static void main(String[] args) {
        P5847 p5847= new P5847();
        p5847.findFarmland(new int[][]{{0,1},{1,0}});
    }
    public int[][] findFarmland(int[][] land) {

        List<List<Integer>> list = new ArrayList<>();
        int w = land[0].length;
        int h = land.length;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (land[i][j] == 1) {
                    //开始画矩形框
                    int x = j;//横向
                    while (x + 1 < w && land[i][x + 1] == 1) {
                        x++;
                    }
                    int y = i;
                    while (y + 1 < h && land[y + 1][x] == 1) {
                        y++;
                    }

                    list.add(Arrays.asList(i,j,y,x));
                    fill(land,i,j,y,x);
                }
            }
        }

        int[][] res = new int[list.size()][4];
        for (int i = 0; i < res.length; i++) {
            List<Integer> p = list.get(i);
            res[i] = new int[]{p.get(0), p.get(1), p.get(2), p.get(3)};
        }

        return res;
    }

    public void fill(int[][] land, int i1, int j1, int i2, int j2) {
        for (int i = i1; i <= i2; i++) {
                for(int j=j1;j<=j2;j++ ){
                    land[i][j]=0;
                }
        }
    }


}
